package com.yantong.service.impl;

import com.yantong.pojo.Industry;
import com.yantong.mapper.IndustryMapper;
import com.yantong.service.IndustryService;
import com.yantong.constant.BusinessCode;
import com.yantong.exception.BDException;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 *  请填写IndustryServiceImpl的描述
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/6/8 16:48
 */
@Service
public class IndustryServiceImpl implements IndustryService {

	@Autowired
	private IndustryMapper industryMapper;

	@Override
	public int saveIndustry(Industry industry) {
		if (industry == null) {
			return 0;
		}
		Industry industryOld = industryMapper.selectByPrimaryKey(industry.getId());
		if (industryOld != null) {
			throw new BDException(BusinessCode.CENTRE_TERMINAL_EXIST);
		}
		return industryMapper.insertSelective(industry);
	}

	@Override
	public PageUtils<Industry> listIndustry(Industry industry, int offset, int limit) {
		PageUtils<Industry> pageUtils = new PageUtils<>();
		int total = industryMapper.countByAll(industry);
		if (total > 0) {
			pageUtils.setTotal(total);
			pageUtils.setRows(industryMapper.listByAll(industry,new RowBounds(offset, limit)));
		}
		return pageUtils;
	}

	@Override
	public int updateIndustry(Industry industry) {
		if (industry == null || industry.getId() == null) {
			return 0;
		}
		return industryMapper.updateByPrimaryKeySelective(industry);
	}

	@Override
	public Industry getIndustryById(Long id) {
		if (id == null) {
			return null;
		}
		return industryMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Industry> selectByList(HashMap hashMap) {
		return industryMapper.selectByList(hashMap);
	}

	@Override
	public int removeProductGrade(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return 0;
		}
		return industryMapper.deleteByPrimaryKey(id);
	}
}
